angular.module('meHistoryOrderDetailcontrollers', [])

  .controller('MeHistoryOrderDetailCtrl', function ($scope, $ionicActionSheet, $state, $timeout,Toast, Storage,MeRecommendList, MeHistoryOrderDetail,MeHistoryOrderDel,History,ENV) {

    var memberId;
    var addWineList;
    var mrwId;
    $scope.ENV = ENV;
    $scope.isShareShow = ENV.isShowShareWindow;//判断是否显示分享框

    $scope.showStatus = false;
    $scope.isShowCheckbox = false;
    $scope.isEmptyWineList = false;

    $scope.order = {
      mrw_name : ''
    };
    // 返回上一页
    $scope.goback = function(){
      History.goback();
    };
    $scope.changeStatus = function () {
      $scope.showStatus = !$scope.showStatus;
    };

    //进入界面时，加载历史酒单详情数据
    $scope.$on('$ionicView.beforeEnter', function () {
      memberId = Storage.get("member").member_id;
      mrwId = MeHistoryOrderDetail.getMrwId();
      //查找数据库的酒单详情
      MeHistoryOrderDetail.getOrderDetail(mrwId,memberId);
      $scope.isShowCheckbox = false;
    });
    $scope.$on('Me.getOrderDetail', function () {
      var result = MeHistoryOrderDetail.getOrderDetailJson();
      // 将信息显示在界面上
      $scope.mrw_name = result.mrw_name;
      $scope.order.mrw_name = result.mrw_name;
      // 获取修改时需要 添加的酒款
      addWineList = MeRecommendList.getSelectWineForAdd();
      if(addWineList && addWineList.length>0){
        if(result.wineList == null){
          result.wineList = [];
        }
        var tempWineList = result.wineList.concat(addWineList);
        result.wineList = uniqueWine(tempWineList);
        MeRecommendList.setSelectWineForAdd([]);
      }
      if(result.wineList == null){
        //如果没有酒款的话，就不显示酒款
        $scope.isEmptyWineList = true;
      }else{
        $scope.isEmptyWineList = false;
        $scope.wineList = result.wineList;
      }
    });

    // 修改选中状态
    $scope.changeSelectState = function (index) {
      $scope.wineList[index].selected = !$scope.wineList[index].selected;
    };

    // 点击删除按钮
    $scope.clickDeleteWineDetail = function () {
      //将需要的数据set到history-order-del对应的service
      MeHistoryOrderDel.setMrwId(mrwId);
      MeHistoryOrderDel.setOrderDetailName($scope.mrw_name);
      MeHistoryOrderDel.setOrderWineList($scope.wineList);
      //跳转到删除酒款界面
      $state.go('history-order-del');
      $scope.showStatus = !$scope.showStatus;
    };


    // 点击增加按钮
    $scope.clickAddWineDetail = function () {
      //跳转到酒款列表界面recommend-list
      MeHistoryOrderDetail.setAddMoreWineStatus(true);
      //将当前的酒单信息保存
      MeHistoryOrderDetail.setMrwId(mrwId);
      MeHistoryOrderDetail.setOrderWineList($scope.wineList);
      $state.go('recommend-list');
      $scope.showStatus = !$scope.showStatus;
    };
    //生成新的推荐酒单
    $scope.createNewWineOrder = function () {
      if($scope.wineList == null){
        Toast.showMessage("酒款列表不能为空");
      }else if($scope.order.mrw_name == ""){
        Toast.showMessage("酒单名不能为空");
      }
      else{
        MeHistoryOrderDetail.insertNewOrder(memberId,$scope.order.mrw_name,$scope.wineList);
      }
    };

    $scope.$on('Me.insertNewOrder', function () {
      var result = MeHistoryOrderDetail.getInsertNewOrder();
      if(result.historyStatus){
        //跳转到历史酒单列表界面
        $state.go('history-list');
        if(addWineList && addWineList.length>0){
          var deleteWineIdList = [];
          for(i=0;i<addWineList.length;i++){
            deleteWineIdList.push(addWineList[i].mnw_id);
          }
          MeRecommendList.deleteWine(memberId,deleteWineIdList);
        }
      }else{
        Toast.showMessage(result.historyMsg);
      }
    });

    //改变数字的显示，添加逗号分割
    $scope.getFormatNum = function (num) {
      return formatNum(num);
    };
  });
